package cc.bookcloud.tool.ocr.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Desc: ocr协议内容解析类
 * @Version: 2.0
 * @Author: John Goo
 * @Date: 2024/11/21 10:01
 **/
public class OCRContentParseUtil {


    /**
     * @Desc：简单解析json内容，去除冗余
     * @Author：John Goo
     * @Date：2024/11/21 10:42
     * @param jsonStr
     * @return JSONObject
    */
    public static JSONObject parseSimple(String jsonStr){
        JSONObject data = JSON.parseObject(jsonStr, Feature.OrderedField);
        // result 结果
        JSONObject result = new JSONObject();
        result.put("code",0);
        JSONArray retArray = new JSONArray();
        result.put("data",retArray);
        // 遍历json
        Set set = data.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()){
            Map.Entry entry = (Map.Entry) it.next();
                String key = (String) entry.getKey();
                // 标识有用的内容
                if(key.contains("IDX_")){
                    String value = (String) entry.getValue();
                    JSONArray array = JSONArray.parseArray(value);
                // 解析内容
                String  rowContent = array.getJSONArray(1).getString(0);
                JSONObject row = new JSONObject();
                row.put(key,rowContent);
                retArray.add(row);
            }
        }
        System.out.println("==============================");
        System.out.println(result);
        return result;
    }


    /**
     * @Desc：简单解析json内容，去除冗余
     * @Author：John Goo
     * @Date：2024/11/21 10:42
     * @param jsonStr
     * @return JSONObject
     */
    public static JSONObject parsexxx(String jsonStr){
        JSONObject data = JSON.parseObject(jsonStr, Feature.OrderedField);
        // result 结果
        JSONObject result = new JSONObject();
        result.put("code",0);
        JSONArray retArray = new JSONArray();
        result.put("data",retArray);
        // 遍历json
        Set set = data.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()){
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            // 标识有用的内容
            if(key.contains("IDX_")){
                String value = (String) entry.getValue();
                JSONArray array = JSONArray.parseArray(value);
                // 解析内容
                String  rowContent = array.getJSONArray(1).getString(0);
                JSONObject row = new JSONObject();
                row.put(key,rowContent);
                retArray.add(row);
            }
        }
        System.out.println("==============================");
        System.out.println(result);
        return result;
    }
}
